import Cookies from 'js-cookie';
const TokenKey = 'Admin-Token';
const UserKey = 'User-Token';

export function getToken() {
  return Cookies.get(TokenKey);
}

export function setToken(token) {
  return Cookies.set(TokenKey, token);
}

export function getUserInfo() {
  let userInfo = Cookies.get(UserKey);
  if (userInfo) userInfo = JSON.parse(userInfo);
  return userInfo;
}

export function setUserInfo(userInfo) {
  return Cookies.set(UserKey, userInfo);
}

export function removeToken() {
  return Cookies.remove(TokenKey);
}

export function removeUserInfo() {
  return Cookies.remove(UserKey)
}

export function getUserMenusList() {
  let menu = getUserInfo().menus;

  let userMenuList = [];
  menu.forEach(function (item) {
    let childrens = item.children;
    if (childrens && childrens.length) {
      childrens.forEach(function (childrenItem) {
        userMenuList.push(`/${item.path}/${childrenItem.path}`);
      });
    } else {
      userMenuList.push(`/${item.id}/index`);
    }
  });
  return userMenuList;
}
export function getFirstMenus() {
  let menus=getUserInfo();
  let lj=menus.menus[0].children[0].show;//true或者false
  let zlj;
  if(lj==false){
    zlj=menus.menus[0].path;
  }else{
    zlj=menus.menus[0].path+'/'+menus.menus[0].children[0].path;
  }
  return zlj;
}

export function getRouters() {
  let menus = getUserInfo().menus;
  let routerArr = [];
  menus.forEach(function (panertRoute) {
    let router = {
      path: '/' + panertRoute.path,
      name: panertRoute.name,
      redirect: panertRoute.redirect,
      meta: panertRoute.meta,
      component: () => import('@/layout'),
      children: []
    };
    let mobileRouter = {
      path: '/mobile/' + panertRoute.path,
      name: 'mobile-' + panertRoute.name,

      meta: panertRoute.meta,
      component: () => import('@/layout'),
      children: []
    };

    let childrens = panertRoute.children;
    childrens.forEach(function (childRoute) {
      let childrenObj = {
        path: childRoute.path,
        name: childRoute.name,
        meta: childRoute.meta,
        component: () => import('@/views' + childRoute.url)
      };
      let mobileChildrenObj = {
        path: childRoute.path,
        name: 'mobile-' + childRoute.name,
        meta: childRoute.meta,
        component: () => import('@/views/mobile' + childRoute.url)
      };
      router.children.push(childrenObj);
      mobileRouter.children.push(mobileChildrenObj);
    });
    routerArr.push(router);
    routerArr.push(mobileRouter);
  });
  return routerArr;


}
